package com.ruoyi.web.controller.community;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.community.domain.BbsArticleComment;
import com.ruoyi.community.domain.Remind;
import com.ruoyi.community.domain.RemindAction;
import com.ruoyi.community.service.IBbsArticleCommentService;
import com.ruoyi.community.utils.MessageUtil;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/bbs")
public class BbsArticleCommentController extends BaseController {
    @Autowired
    private IBbsArticleCommentService bbsArticleCommentService;
    @Autowired
    private ISysUserService sysUserService;

    private String toUrl = "/pages/post/post_content/post_content?articleid=";

    @GetMapping("/commentList/{articleid}")
    public TableDataInfo commentList(@PathVariable Long articleid){
        startPage();
        Integer communityid = SecurityUtils.getCommunityId();
        List<BbsArticleComment> list = bbsArticleCommentService.listCommentByArticleId(communityid,articleid);
        return getDataTable(list);
    }
    @PostMapping("/commentInsert")
    public AjaxResult insert(@RequestBody  BbsArticleComment bbsArticleComment){
        int res = bbsArticleCommentService.insert(bbsArticleComment);
        System.out.println(bbsArticleComment.getCommenterid());
        System.out.println(SecurityUtils.getUserID());
        //  自己对自己评论不用发送站内信
        if(res == 1 && (!bbsArticleComment.getCommenterid().equals(SecurityUtils.getUserID()))){
            Remind remind = new Remind();
            remind.setSenderId(SecurityUtils.getUserID());
            String username = sysUserService.returnNameByUserid(SecurityUtils.getUserID());
            remind.setSenderName(username);
            remind.setSenderAction(RemindAction.PINGLUN);
            remind.setUrl(toUrl+bbsArticleComment.getArticleid());
            remind.setRecipientId(bbsArticleComment.getCommentedid());
            if(bbsArticleComment.getParentcommentid() == -1){
                remind.setContent(username + "在您的文章中评论。");
            } else {
                remind.setContent(username + "回复您的评论。");
            }
            remind.setContentType("在论坛中评论");
            MessageUtil.addRemind(remind);
        }
        return toAjax(res);
    }
    @PutMapping("commentUpdateDelete")
    public int updateDelete(@RequestBody BbsArticleComment bbsArticleComment){
        int res = bbsArticleCommentService.update(bbsArticleComment);
        return res;
    }
    @DeleteMapping("delete/{commentid}")
    public AjaxResult delete(@PathVariable Long commentid){
        return toAjax(bbsArticleCommentService.delete(commentid));
    }
    @GetMapping("/getCommentList/{articleid}")
    public TableDataInfo getCommentList(@PathVariable Long articleid){
        startPage();
        Integer communityid = SecurityUtils.getCommunityId();
        List<BbsArticleComment> list = bbsArticleCommentService.listComments(communityid,articleid);
        return getDataTable(list);
    }
    @GetMapping("/getAllChildComment/{commentid}")
    public TableDataInfo getAllChildComment(@PathVariable Long commentid){
        startPage();
        List<BbsArticleComment> list = bbsArticleCommentService.listAllComments(commentid);
        return getDataTable(list);
    }

}
